home *** CD-ROM | disk | FTP | other *** search
Text File | 1992-12-10 | 2.9 KB | 89 lines | [TEXT/PJMM] |
- program KenExampleCode;
-
- { You can do anything you want with this code - if you can make any money out of it, you'll be doing well! }
-
- uses
- TCPTypes, TCPStuff, TCPConnections;
-
- const
- user_name = 'peter';
- dest_name = 'cujo.curtin.edu.au';
- dest_port = 79;
- nul = chr(0);
- lf = chr(10);
- cr = chr(13);
-
- var
- oe: OSErr;
- cp: connectionIndex;
- quitNow: boolean;
- cer: connectionEventRecord; { Event record for TCP events, simmilar to EventRecord }
- s: str255;
- count: longInt;
- gotlinefeed: boolean;
- begin
- ShowText;
- quitNow := false;
- oe := InitConnections; { Startup the TCP units }
- if oe = noErr then begin
- oe := FindAddress(cp, dest_name, nil);
- if oe = noErr then begin
- count := 0;
- while not quitNow do begin
- if GetConnectionEvent(any_connection, cer) then begin { Get the next TCP event }
- case cer.event of
- C_Found: begin
- writeln('Found ', dest_name, ' has address ', pointer(cer.value));
- oe := NewActiveConnection(cp, Default_TCPBUFFERSIZE, cer.value, dest_port, nil);
- { Open an active connection to the Finger port on dest_name }
- end;
- C_SearchFailed: begin
- writeln('Couldn''t fine the address for ', dest_name);
- quitNow := true;
- end;
- C_Established: begin { Happens once per succesful connection establishment }
- writeln('Connection Established');
- s := concat(user_name, cr, lf); { Send the line that finger wants, complete with crlf }
- oe := TCPSend(cer.tcpc, @s[1], length(s), true);
- writeln('Name sent with error ', oe);
- if oe <> noErr then
- CloseConnection(cp); { Better close the connection if we can't send anything to it! }
- end;
- C_FailedToOpen:
- writeln('Ooops, connection failed to open... Error is ', cer.value, ' Timed out is ', cer.timedout);
- { Example, network unreachable etc. Error code is in cer.value }
- C_Closing: begin
- writeln('Connection closing');{ Gets called when the connection starts closing down}
- CloseConnection(cer.connection); { Close our side of the connection }
- end;
- C_Closed: begin
- writeln('Connection closed, quit now');
- quitNow := true; { The connection is closed, quit the program }
- end;
- C_CharsAvailable: begin
- {$PUSH}
- {$R-}
- oe := TCPReceiveUpTo(cer.tcpc, 10, 60, @s[1], 255, count, gotlinefeed);
- { Recieve characters up to a line feed }
- if (count > 0) & (s[count] = lf) then { strip off linefeed }
- count := count - 1;
- if (count > 0) & (s[count] = cr) then { strip off cr }
- count := count - 1;
- s[0] := chr(count);
- {$POP}
- if gotlinefeed then begin { if we got a linefeed, print the string, otherwise go round again and wait for more characters }
- writeln(s);
- count := 0;
- end;
- end;
- end;
- end;
- end;
- end;
- FinishEverything; { Close everything, clean up }
- { ALWAY CALL THIS, OR YOU WILL BE SORRY! }
- end;
- writeln('Click to quit');
- while not Button do
- ;
- end.